<?php
namespace Index\Controller;
use Think\Controller;
class RegisterController extends Controller {
    public function indexAction(){
        if(IS_POST){

        	$username= I('post.name');
        	$password = md5(I('posr.password'));
        	$email = I('post.mail');

            $reg_time= time();
            $reg_ip=$_SERVER["REMOTE_ADDR"];

        	

        	$sql = "insert into user (username,password,email,reg_time,reg_ip) value('{$username}','{$password}','{$email}','{$reg_time}','{$reg_ip}')";

        	$res = M()->execute($sql);

        	if($res){
        		$this->success('注册成功，请登录',U('index/login/index'));
        	}else{
        		$this->error('注册失败，请重新注册',U('index/login/index'));
        	}




        }else{
        	$this->error('非法请求，请先注册',U('index/login/index'));
        }
    }

    public function checkdataAction()
    {
        if(IS_AJAX){
            $pregs = array(
                'name'=>array(
                    'preg'=>"/^[a-z][0-9a-z_]{3,7}$/i",
                    'info'=>'用户名必须是4-8位字母'
                    ),
                'mail'=>array(
                    'preg'=>"/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i",
                    'info'=>'邮箱格式不对',
                    ),
                'password'=>array(
                    'preg'=>"/^[a-z0-9]{4,8}$/i",
                    'info'=>'密码必须是4-8位字母或数字',
                    ),
                );

            if($_POST){


                $key = array_keys($_POST);

                $data= isset($_POST[$key[0]]) ? $_POST[$key[0]] : '';

                if($key[0]=='mail'){

                    $sql = "select * from user where email='{$data}'";

                    $res = M()->query($sql);

                    if($res){
                        $arr = array(
                            'error'=>'0',
                            'info'=>'邮箱已经被注册'
                            );
                        echo json_encode($arr);
                        exit;
                    }
                }


                if($key[0]=='captcha'){

                    $this->__checkcaptcha($data);

                }else{
                     $preg = $pregs[$key[0]]['preg'];

                    // var_dump($data,$preg);

                    $c = preg_match($preg,$data);

                    if($c){
                        $arr = array(
                            'error'=>'1',
                            'info'=>$data
                            );

                        echo json_encode($arr);
                    }else{
                        $arr = array(
                            'error'=>'0',
                            'info'=>$pregs[$key[0]]['info']
                            );
                        echo json_encode($arr);
                    }
                }

            }

        }
    }

    public function __checkcaptcha($code=null)
    {
        if(IS_AJAX){

            $verify = new \Think\Verify();
            $verify->reset = false;

            if($verify->check($code)){
                $arr = array(
                    'error'=>'1',
                    'info'=>$code
                    );

                echo json_encode($arr);
            }else{
                $arr = array(
                    'error'=>'0',
                    'info'=>'验证不对'
                    );
                echo json_encode($arr);
            }


        }
    }
}